ig.module(
    
    'game.entities.roundfollower'
    
).requires(
    
    'impact.entity',
    'game.entities.particle'
    
).defines(function(){

    EntityRoundFollower = ig.Entity.extend({
        size:{
            x: 8,
            y: 8
        },
        offset:{
            x: 0,
            y: 0
        },
        maxVel: {
            x: 180,
            y: 180
        },
        player: null,
        
        type: ig.Entity.TYPE.B,
        
        checkAgainst: ig.Entity.TYPE.A,
        
        health: 10,
        
        animSheet: new ig.AnimationSheet('media/sprites/factory.png',8,8),
        
        stateTimer: null,
        moveTimer: null,
        
        prevState: 'idle',
        curState: 'idle',
        
        init: function( x, y, settings){
            this.parent(x, y, settings);
            
            this.addAnim('idle', 1, [0]);
            this.stateTimer = new ig.Timer( 1 );
            this.moveTimer = new ig.Timer( 1 );
            this.curState = 'idle';
        },        
        
        update: function(){
            
            if( this.stateTimer.delta() > 5 ){
                if( this.curState == 'idle' ){                    
                    this.curState = 'atack';
                    this.stateTimer.set( 0 );
                } else if( this.curState == 'atack' ){
                    this.curState = 'idle';                    
                    this.stateTimer.set( 0 );
                }                             
            }
            
            if( this.curState == 'idle' ){
                
                var rx = this.player.pos.x - this.pos.x;
                var ry = this.player.pos.y - this.pos.y;
               
                this.vel.x = -ry;
                this.vel.y = rx;
                
                var cs = Math.cos( this.moveTimer.delta() );
                if(cs < 0){
                    //(-y,x)
                    this.addVel(-this.vel.y * cs, this.vel.x * cs * Math.random()*2);
                } else {
                    //(y,-x)
                    this.addVel(this.vel.y * cs, -this.vel.x * cs * Math.random()*2);
                }                
                
            } else if( this.curState == 'atack' ){
                
                this.vel.x = (this.player.pos.x - (this.pos.x + this.size.x/2)) * 2;
                this.vel.y = (this.player.pos.y - (this.pos.y + this.size.y/2)) * 2;
            }
           
            this.parent();
        },
        addVel: function(dx,dy){
            this.vel.x += dx;
            this.vel.y += dy;
        },
        kill: function(){
            var gibs = ig.ua.mobile ? 5 : 30;
            for (var i = 0; i < gibs; i++) {
                ig.game.spawnEntity(EntityRandomFollowerGib, this.pos.x, this.pos.y);
            }
            this.parent();
        },
        check: function( other ){
            
            other.receiveDamage( 10 );
            
            this.kill();
        }
    });
    
    EntityRandomFollowerGib = EntityParticle.extend({
        lifetime: 3,
        fadetime: 6,
        friction: {
            x: 0,
            y: 0
        },
        vel: {
            x: 60,
            y: 150
        },
        animSheet: new ig.AnimationSheet('media/sprites/blob-gibs.png', 4, 4),
        init: function (x, y, settings) {
            this.addAnim('idle', 0.1, [0, 1, 2]);
            this.parent(x, y, settings);
        }
    });
})